import React, {Component} from 'react';
import {connect} from 'dva';
import moment from 'moment';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Icon,
  Dropdown,
  Popover,
  InputNumber,
  DatePicker,
  message,
  Badge,
  Divider,
  Steps,
  Upload,
  Collaps, Empty,Tree
} from 'antd';
const { TreeNode } = Tree;

import {Select,Button , Table ,Modal} from '@/components/CustomAntd'
import permission from "../../../locales/default/permission";
@Form.create()
export default class PermisstionModal extends Component {
  componentDidMount() {

  }
  renderTreeNodes = (data) => {
    const content = [];
    data.map(item => {
      if (item.permissions && item.permissions.length>0) {
        content.push(
          <TreeNode title={item.permission_name} key={item.permission_id} dataRef={item}>
            {this.renderTreeNodes(item.permissions)}
          </TreeNode>
        );
      }else {
        content.push (<TreeNode  title={item.permission_name} key={item.permission_id} dataRef={item} />);
      }
    });
    return content
  }
  onCheck = (checkedKeys,checkNode) => {
    /*let permission = this.props.permission ?  this.props.permission :[];
    let href_checked_permissions = this.props.href_checked_permissions ?this.props.href_checked_permissions :[];
    permission = [...checkedKeys];
    href_checked_permissions = checkNode.halfCheckedKeys;*/
    this.props.saveState({
      checked_permissions : checkedKeys,
      href_checked_permissions : checkNode.halfCheckedKeys
    })
  };
  render() {
    const {visible,modalHandleCancel,checked_permissions,saveState,permission,href_checked_permissions,updateRoleId,subPermisstion} = this.props;
    const onOk = e=>{
      e.preventDefault();
      let stringMap ='';
      const halfCheck = href_checked_permissions?href_checked_permissions:[];
      const sub_permisson = checked_permissions.concat(halfCheck);
      for(let i=0; i<sub_permisson.length; i++){
        if(sub_permisson[i] === 'all'){//删除 'all'
          sub_permisson.splice(i,1);
          continue;
        }
      }
      stringMap = sub_permisson.join(',');


      let subFields = {
        role_id:updateRoleId,
        permission_id:stringMap,
        //is_left_menu : '1,4',
      }
      subPermisstion(subFields)
    }
    return (
      <Modal
        title='修改权限'
        width={1000}
        visible={visible}
        onOk={onOk}
        onCancel={modalHandleCancel}
      >
        <form layout="inline" >
          <Tree
            style={{marginLeft : '25px'}}
            checkable
            defaultExpandAll={true}
            onCheck={(checked,checkNode)=>this.onCheck(checked,checkNode)}
            checkedKeys={checked_permissions}
          >
            <TreeNode title={`全选`} key={`all`} dataRef={``}>
              {this.renderTreeNodes(permission)}
            </TreeNode>
          </Tree>
        </form>


      </Modal>
    );
  }
}
